package com.yidu.ReportManagement.controller;

import com.yidu.DatelessManagement.entity.NetWorth;
import com.yidu.GlobalManagement.log.util.DateTimeUtil;
import com.yidu.ReportManagement.entity.FundInvestPlateTable2;
import com.yidu.ReportManagement.entity.FundPortfolioTable;
import com.yidu.ReportManagement.service.FundPortfolioTableService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 任俊杰
 * @version 1.0
 * @date 2023/2/10
 */
@RestController
@RequestMapping("fundInvestPlateTables")
public class FundPortfolioTableController{
    @Resource
    private FundPortfolioTableService fundPortfolioTableService;

    @RequestMapping("selectFundPortfolioTable")
    @ResponseBody
    public Object selectFundPortfolioTable(String Date,String fundId) throws ParseException {
        Date = Date.trim();
        fundId=fundId.trim();
        if(Date.equals("")){
            Date= DateTimeUtil.getSystemDateTime("yyyy-MM-dd");
        }
        Date=Date.trim();
        //行情数据周六周日特殊处理模块
        String dateTimeTwo=Date;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = df.parse(Date);
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int i = instance.get(Calendar.DAY_OF_WEEK);
        i--;
        if(i==6){
            instance.add(Calendar.DATE, -1); //得到前一天
            Date time = instance.getTime();
            dateTimeTwo=df.format(time);

        }else if(i==7){
            instance.add(Calendar.DATE, -2); //得到前一天
            Date time = instance.getTime();
            dateTimeTwo=df.format(time);
        }
        dateTimeTwo=dateTimeTwo.trim();



        //查询当日资产净值
        NetWorth netWorth = fundPortfolioTableService.selectValue(Date, fundId);

        //查询证券板块
        List<FundPortfolioTable> fundInvestPlateTables = fundPortfolioTableService.selectFundPortfolioTable(Date, fundId, dateTimeTwo);
        //遍历板块集合，生成页面需要的对象
        if(fundInvestPlateTables.size()!=0){

                for (FundPortfolioTable fundInvestPlateTable : fundInvestPlateTables) {
                    if(fundInvestPlateTable!=null){
                        //计算市值净值百分比
                        if(netWorth!=null){
                            if(netWorth.getCapital()!=0){
                                fundInvestPlateTable.setMarketValueStatistics(fundInvestPlateTable.getMarketValue()/netWorth.getCapital());
                            }
                        }
                    }
                }
            }



        Map<String,Object> josn = new HashMap<String,Object>();
        josn.put("code", 0);
        josn.put("count",fundInvestPlateTables.size());
        josn.put("msg", "");
        josn.put("data", fundInvestPlateTables);
        return josn;


    }


    @RequestMapping("selectFundPortfolioTable2")
    @ResponseBody
    public Object selectFundPortfolioTable2(String Date,String fundId) throws ParseException {
        //去空格
        Date = Date.trim();
        fundId=fundId.trim();
        //如页面传空字串，则获取本地时间
        if(Date.equals("")){
            Date= DateTimeUtil.getSystemDateTime("yyyy-MM-dd");

        }
        Date=Date.trim();
        //行情数据周六周日特殊处理模块
        String dateTimeTwo=Date;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = df.parse(Date);
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int i = instance.get(Calendar.DAY_OF_WEEK);
        i--;
        if(i==6){
            instance.add(Calendar.DATE, -1); //得到前一天
            Date time = instance.getTime();
            dateTimeTwo=df.format(time);

        }else if(i==7){
            instance.add(Calendar.DATE, -2); //得到前一天
            Date time = instance.getTime();
            dateTimeTwo=df.format(time);
        }
        dateTimeTwo=dateTimeTwo.trim();


        List josnList =new ArrayList();
        //查询当日资产净值
        NetWorth netWorth = fundPortfolioTableService.selectValue(Date,fundId);

        //查询证券板块
        List<FundPortfolioTable> fundInvestPlateTables =fundPortfolioTableService.selectFundPortfolioTable(Date,fundId,dateTimeTwo);
        if(fundInvestPlateTables.size()!=0){
            for (FundPortfolioTable fundInvestPlateTable : fundInvestPlateTables) {
                if(fundInvestPlateTable!=null){
                    //计算市值占净值百分比
                    if(netWorth!=null){
                        if(netWorth.getCapital()!=0){
                            fundInvestPlateTable.setMarketValueStatistics(fundInvestPlateTable.getMarketValue()/netWorth.getCapital());
                        }
                    }
                }
            }
        }
        //遍历板块集合，生成页面需要的对象
        if(fundInvestPlateTables.size()!=0){
            for (FundPortfolioTable fundInvestPlateTable : fundInvestPlateTables) {
                if(fundInvestPlateTable!=null){
                    FundInvestPlateTable2 fundInvestPlateTable2=new FundInvestPlateTable2(fundInvestPlateTable.getPlateName(),fundInvestPlateTable.getMarketValue());
                    josnList.add(fundInvestPlateTable2);
                }
            }
        }
        return josnList;
    }
}
